<!DOCTYPE html>

<html>

<head>

    <!-- 页面meta -->

    <meta charset="utf-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <title></title>

    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">

    <!-- 引入样式 -->

    <link rel="stylesheet" href="../../plugins/elementui/index.css">

    <link rel="stylesheet" href="../../plugins/font-awesome/css/font-awesome.min.css">

    <link rel="stylesheet" href="../../css/style.css">

</head>

<body class="hold-transition">

<div id="app">


    <div class="app-container">

        <div>

            <div class="filter-container">

                <el-input placeholder="员工名称" v-model="pagination.name" style="width: 200px;"
                          class="filter-item"></el-input>

                <el-select v-model="pagination.sex" placeholder="性别">
                    <el-option label="" value=""></el-option>
                    <el-option label="男" value="male"></el-option>
                    <el-option label="女" value="female"></el-option>
                </el-select>
                <el-input placeholder="部门" v-model="pagination.department" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-input placeholder="职位" v-model="pagination.position" style="width: 200px;"
                          class="filter-item"></el-input>

                <el-button @click="getName()" class="dalfBut">查询</el-button>

                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                <el-button type="primary" id="btn_export" @click="exportExecl()">打印报表</el-button>

            </div>

            <el-table size="medium" current-row-key="id" :data="dataList" stripe highlight-current-row>

                <el-table-column type="index" align="center" label="序号"></el-table-column>

                <el-table-column prop="name" label="员工姓名" align="center"></el-table-column>

                <el-table-column prop="sex" label="性别" align="center"></el-table-column>

                <el-table-column prop="age" label="年龄" align="center"></el-table-column>

                <el-table-column prop="department" label="部门" align="center"></el-table-column>

                <el-table-column prop="position" label="职位" align="center"></el-table-column>

                <el-table-column prop="phone" label="联系电话" align="center"></el-table-column>

                <el-table-column prop="time" label="入职时间" align="center"></el-table-column>

                <el-table-column label="操作" align="center">

                    <template slot-scope="scope">

                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>

                        <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>

                    </template>

                </el-table-column>

            </el-table>

            <!-- 新增标签弹层 -->

            <div class="add-form">

                <el-dialog title="新增员工" :visible.sync="dialogFormVisible">

                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">

                        <el-row>

                            <el-col :span="12">

                                <el-form-item label="员工姓名" prop="name">

                                    <el-input v-model="formData.name"/>

                                </el-form-item>

                            </el-col>

                            <el-col :span="12">

                                <el-form-item label="性别" prop="sex">

                                    <el-input v-model="formData.sex"/>

                                </el-form-item>

                            </el-col>

                        </el-row>
                        <el-row>

                            <el-col :span="12">

                                <el-form-item label="年龄" prop="age">

                                    <el-input v-model="formData.age"/>

                                </el-form-item>

                            </el-col>

                            <el-col :span="12">

                                <el-form-item label="部门" prop="department">

                                    <el-input v-model="formData.department"/>

                                </el-form-item>

                            </el-col>

                        </el-row>
                        <el-row>

                            <el-col :span="12">

                                <el-form-item label="职位" prop="position">

                                    <el-input v-model="formData.position"/>

                                </el-form-item>

                            </el-col>

                            <el-col :span="12">

                                <el-form-item label="联系电话" prop="phone">

                                    <el-input v-model="formData.phone"/>

                                </el-form-item>

                            </el-col>

                        </el-row>
                        <el-row>
                            <el-col>
                                <el-form-item label="入职时间" prop="time">
                                    <div class="block">
                                        <el-date-picker
                                                v-model="formData.time"
                                                align="right"
                                                type="date"
                                                placeholder="选择日期"
                                                :picker-options="pickerOptions">
                                        </el-date-picker>
                                    </div>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>

                    <div slot="footer" class="dialog-footer">

                        <el-button @click="dialogFormVisible = false">取消</el-button>

                        <el-button type="primary" @click="handleAdd()">确定</el-button>

                    </div>

                </el-dialog>

            </div>
            <!-- 编辑标签弹层 -->

            <div class="add-form">

                <el-dialog title="编辑检查项" :visible.sync="dialogFormVisible4Edit">

                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">

                        <el-row>

                            <el-col :span="12">

                                <el-form-item label="员工姓名" prop="name">

                                    <el-input v-model="formData.name"/>

                                </el-form-item>

                            </el-col>

                            <el-col :span="12">

                                <el-form-item label="性别" prop="sex">

                                    <el-input v-model="formData.sex"/>

                                </el-form-item>

                            </el-col>

                        </el-row>

                        <el-row>

                            <el-col :span="12">

                                <el-form-item label="年龄" prop="age">

                                    <el-input v-model="formData.age"/>

                                </el-form-item>

                            </el-col>

                            <el-col :span="12">

                                <el-form-item label="部门" prop="department">

                                    <el-input v-model="formData.department"/>

                                </el-form-item>

                            </el-col>

                        </el-row>

                        <el-row>

                            <el-col :span="12">

                                <el-form-item label="职位" prop="position">

                                    <el-input v-model="formData.position"/>

                                </el-form-item>

                            </el-col>

                            <el-col :span="12">

                                <el-form-item label="联系电话" prop="phone">

                                    <el-input v-model="formData.phone"/>

                                </el-form-item>

                            </el-col>

                        </el-row>
                        <el-row>
                            <el-col>
                                <el-form-item label="入职时间" prop="time">
                                    <div class="block">
                                        <el-date-picker
                                                v-model="formData.time"
                                                align="right"
                                                type="date"
                                                placeholder="选择日期"
                                                :picker-options="pickerOptions">
                                        </el-date-picker>
                                    </div>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>

                    <div slot="footer" class="dialog-footer">

                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>

                        <el-button type="primary" @click="handleEdit()">确定</el-button>

                    </div>
                </el-dialog>

            </div>
            <!--分页工具条-->
            <div class="block">
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page.sync="currentPage"
                        :page-size="pagesize"
                        layout="total, prev, pager, next, jumper"
                        :total="totalCount">
                </el-pagination>
            </div>
        </div>

    </div>

</div>

</body>

<!-- 引入组件库 -->

<script src="../../js/vue.js"></script>

<script src="../../plugins/elementui/index.js"></script>

<script type="text/javascript" src="../../js/jquery.min.js"></script>

<script src="../../js/axios-0.18.0.js"></script>

<script>
    new Vue({
        el: '#app',
        data: {
            pagination: {
                name: '',
                sex: '',
                department: '',
                position: '',
            },
            dataList: [],//当前页要展示的列表数据
            formData: {},//表单数据
            pageData: {},//页码数据
            currentPage: 1,//设置显示当前页码
            pagesize: 5, // 每页显示的条数
            totalCount: 100,
            dialogFormVisible: false,//控制表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            rules: {//校验规则
                name: [{required: true, message: '员工名称为必填项', trigger: 'blur'}],
                sex: [{required: true, message: '性别为必填项', trigger: 'blur'}],
                age: [{required: true, message: '年龄为必填项', trigger: 'blur'}],
                department: [{required: true, message: '部门为必填项', trigger: 'blur'}],
                position: [{required: true, message: '职位为必填项', trigger: 'blur'}],
                phone: [{required: true, message: '联系电话为必填项', trigger: 'blur'}],
                time: [{required: true, message: '入职时间为必填项', trigger: 'blur'}],
            },
            pickerOptions: {
                disabledDate(time) {
                    return time.getTime() > Date.now();
                },
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                        picker.$emit('pick', new Date());
                    }
                }, {
                    text: '昨天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24);
                        picker.$emit('pick', date);
                    }
                }, {
                    text: '一周前',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', date);
                    }
                }]
            },
        },

        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            //this.getAll();
            this.getPage();
        },

        methods: {
            //导出excel汇总表
            exportExecl() {
                //打开新窗口
                window.open("/staffs/makeExcel");
            },
            //分页
            handleSizeChange(val) {
                //console.log(`每页 ${val} 条`);
                // 重新设置每页显示的条数
                this.pagesize = val;
                this.getPage();
            },
            handleCurrentChange(val) {
                //console.log(`当前页: ${val}`);
                // 重新设置当前页码
                this.currentPage = val;
                this.getPage();
            },

            getPage() {
                axios.get("/staffs/" + this.currentPage + "/" + this.pagesize).then((res) => {
                    //console.log(res.data.data);
                    if (res.data.code === 20041) {
                        this.totalCount = res.data.data.total;
                        this.currentPage = res.data.data.pageNum;
                        this.pagesize = res.data.data.pageSize;
                        this.dataList = res.data.data.list;
                    }else {
                        this.$message.error(res.data.msg);
                    }
                })
            },
            //模糊查询
            getName(){
                //发送ajax请求
                axios.post("/staffs/ByName",this.pagination).then((res) => {
                    if(res.data.code === 20041){
                        this.dataList = res.data.data;
                        console.log(this.dataList);
                    }else {
                        this.$message.error(res.data.msg);
                    }
                });
            },
            //列表
            getAll() {
                //发送ajax请求
                axios.get("/staffs").then((res) => {
                    if (res.data.code === 20041) {
                        this.dataList = res.data.data;
                        console.log(res.data.code)
                    } else {
                        this.$message.error(res.data.msg);
                    }
                });
            },

            //弹出添加窗口
            handleCreate() {
                this.dialogFormVisible = true;
                this.resetForm();
            },

            //重置表单
            resetForm() {
                this.formData = {};
            },

            //添加
            handleAdd() {
                //发送ajax请求
                axios.post("/staffs/add", this.formData).then((res) => {
                    console.log(res.data);
                    //如果操作成功，关闭弹层，显示数据
                    if (res.data.code === 20011) {
                        this.dialogFormVisible = false;
                        this.$message.success("添加成功");
                    } else if (res.data.code === 20010) {
                        this.$message.error("添加失败");
                    } else {
                        this.$message.error(res.data.msg);
                    }
                }).finally(() => {
                    this.getPage();
                });
            },

            //弹出编辑窗口
            handleUpdate(row) {
                //编辑表单可见
                //this.dialogFormVisible4Edit = true;
                //查询数据，根据id查询
                axios.get("/staffs/" + row.id).then((res) => {
                    console.log(res.data.msg);
                    if (res.data.code === 20041) {
                        //展示弹层，加载数据
                        this.formData = res.data.data;
                        this.dialogFormVisible4Edit = true;
                    } else {
                        this.$message.error(res.data.msg);
                    }
                });
            },

            //编辑
            handleEdit() {
                //发送ajax请求
                axios.put("/staffs", this.formData).then((res) => {
                    //如果操作成功，关闭弹层，显示数据
                    if (res.data.code === 20031) {
                        this.dialogFormVisible4Edit = false;
                        this.$message.success("修改成功");
                    } else if (res.data.code === 20030) {
                        this.$message.error("修改失败");
                    } else {
                        this.$message.error(res.data.msg);
                    }
                }).finally(() => {
                    this.getPage();
                });
            },

            // 删除
            handleDelete(row) {
                //1.弹出提示框
                this.$confirm("此操作永久删除当前数据，是否继续？", "提示", {
                    type: 'info'
                }).then(() => {
                    //2.做删除业务
                    axios.delete("/staffs/" + row.id).then((res) => {
                        if (res.data.code === 20021) {
                            this.$message.success("删除成功");
                        } else {
                            this.$message.error("删除失败");
                        }
                    }).finally(() => {
                        this.getPage();
                    });
                }).catch(() => {
                    //3.取消删除
                    this.$message.info("取消删除操作");
                });
            }
        }
    })

</script>

</html>